#ifndef _SERIAL_PORT_H
#define _SERIAL_PORT_H

#include <stdio.h>
//#include <time.h>
#include <unistd.h>
//#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <iostream>
using namespace std;

#define EVEN_PARITY 0
#define ODD_PARITY 1
#define NO_PARITY 2
#define SPACE_PARITY 3

#define HARDWARE_FLOW_CONTROL_DISABLE 0
#define HARDWARE_FLOW_CONTROL_ENABLE 1

#define RW_PREVILAGE
#define R_PREVLIAGE
#define W_PREVLIAGE

class Serial_port {

public:
	Serial_port();
	~Serial_port();

	int open_port(char *device_fila_path, int baud_rate, int parity, int data_bits, int stop_bits);
	int set_baud_rate(int baud_rate);
	int set_parity(int parity);
	int set_data_bits(int data_bits);
	int set_stop_bits(int stop_bits);
	int set_time_out(int,double);
	int set_hardware_flow_control(int);
	int set_dtr_pin(int);
	int set_rts_pin(int);

	int close();
	int read_line(unsigned char *read_buffer, int buffer_size);
	int read_binary(unsigned char *read_buffer, int bytes);
	int read_nonblocking(unsigned char * read_buffer, int bytes);
	int write_binary(unsigned char *write_buffer, int data_size);
	int flush();

private:
	int            serial_port_fd;
	struct termios settings;

	int nr_data;
	char buffer[512];

	int set_settings(void);
};

#endif /* _SERIAL_PORT_H */
